package com.suixingpay.demo.service.impl;

import com.suixingpay.demo.entity.KunpengOneAddressA;
import com.suixingpay.demo.entity.KunpengOneAddressC;
import com.suixingpay.demo.entity.KunpengOneUser;
import com.suixingpay.demo.mapper.KunpengOneUserMapper;
import com.suixingpay.demo.service.KunpengOneUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class KunpengOneKunpengOneUserServiceImpl implements KunpengOneUserService {

    @Autowired
    private KunpengOneUserMapper userMapper;

    @Override
    public List<KunpengOneUser> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public String insertOne(KunpengOneUser kunpengOneUser) {
        String legalPersonId = kunpengOneUser.getLegalPersonId();
        String settlementId = kunpengOneUser.getSettlementId();
        String cardNum = kunpengOneUser.getCardNum();
        Integer settlementBankNum = kunpengOneUser.getSettlementBankNum();
        if (settlementBankNum == 1){
            kunpengOneUser.setSettlementBankName("中国工商银行");
        }else {
            kunpengOneUser.setSettlementBankName("中国农业银行");
        }
        kunpengOneUser.setMerchantState(00);
        kunpengOneUser.setWorkOrderCheckState(01);
        kunpengOneUser.setLegalPersonIdMask(legalPersonId.substring(0, 3) + "***********" + legalPersonId.substring(legalPersonId.length() - 4));
        kunpengOneUser.setSettlementIdMask(settlementId.substring(0, 3) + "***********" + settlementId.substring(settlementId.length() - 4));
        kunpengOneUser.setCardNummask(cardNum.substring(0, 6) + "**********" + cardNum.substring(cardNum.length() - 3));
        List<KunpengOneUser> result = userMapper.selectLegalPersonId(legalPersonId);
        if (result.isEmpty()) {
            userMapper.insertOne(kunpengOneUser);
            return "success";
        } else {
            return "身份证号已重复";
        }

    }

    /**
     * 生成工单编号
     */
    @Override
    public String workOrderNum() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("yyyyMMddhhmmss").format(date);
        int sum = 1 + (int) (Math.random() * 90 + 10);
        return "SXF" + dateString + sum;
    }

    @Override
    public List<KunpengOneAddressC> selectFatherAreaNum(String fatherAreaNum) {
        return userMapper.selectFatherAreaNum(fatherAreaNum);
    }

    @Override
    public List<KunpengOneAddressA> selectFatherAreaNumA(String fatherAreaNum) {
        return userMapper.selectFatherAreaNumA(fatherAreaNum);
    }

    @Override
    public KunpengOneUser selectOneAll(String workOrderNum) {
        return userMapper.selectOneAll(workOrderNum);
    }
}
